from django.shortcuts import render,redirect,reverse
from django.http.response import JsonResponse
import string
import random
from django.core.mail import send_mail
from . import models
from .models import CaptchaModel
from django.views.decorators.http import require_http_methods
from .forms import RegisterForm, LoginForm
from django.contrib.auth import get_user_model,login,logout


User = get_user_model()

#登录功能模块
@require_http_methods(['GET','POST'])
def kologin(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        form = LoginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            remember = form.cleaned_data['remember']
            user = User.objects.filter(email=email).first()
            if user and user.check_password(password):
                #登录
                login(request, user)
                #判断是否需要记住我
                if remember:
                    #如果没有点击记住我，那么就要设置过期时间为0，即关闭浏览器后就会过期
                    request.session.set_expiry(0)
                #如果点击了，那么就什么都不做，使用默认的两周过期时间
                return redirect('/')
            else:
                print('邮箱或密码错误！')
                # form.add_error('email',"邮箱或密码错误")
                # return render(request, 'login.html',{'form':form})
                return redirect(reverse('koauth:login'))


#退出登录功能模块
def kologout(request):
    logout(request)
    return redirect( '/')


#注册功能模块
@require_http_methods(['GET','POST'])
def register(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    else:
        form = RegisterForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            User.objects.create_user(username=username, email=email, password=password)
            #跳转到登录界面
            return redirect(reverse('koauth:login'))
        else:
            print(form.errors)
            # return render(request, 'register.html', {'form': form})
            #重新跳转到注册页面
            return redirect(reverse('koauth:register'))


def send_email_captcha(request):
    #发送邮箱验证码，?email=xxx
    email=request.GET.get('email')
    if not email:
        return JsonResponse({"code":400,"message":"必须传递邮箱！"})
    #限制邮箱验证码为随机4位阿拉伯数字
    #['0','6','2','4']
    captcha="".join(random.sample(string.digits,4) )
    #存储到数据库中
    CaptchaModel.objects.update_or_create(email=email,defaults={'captcha':captcha})
    send_mail("kinoko博客注册验证码",message=f"您的注册验证码是{captcha}",recipient_list=[email],from_email=None)
    return JsonResponse({"code":200,"message":"邮箱验证码发送成功!"})
